﻿using WeDonekRpc.Helper;

namespace Basic.UserFormService.Calculate.Extend
{
    public static class QueryTableExtend
    {
        public static decimal Max ( params object[][] list )
        {
            if ( list.IsNull() )
            {
                return decimal.Zero;
            }
            else if ( !CurState.Index.HasValue )
            {
                return decimal.Zero;
            }
            int index = CurState.Index.Value;
            decimal num = decimal.MaxValue;
            foreach ( object[] b in list )
            {
                object i = b[index];
                if ( i == null )
                {
                    continue;
                }
                decimal k = Convert.ToDecimal(i);
                if ( k > num )
                {
                    num = k;
                }
            }
            return num;
        }
        public static decimal Min ( params object[][] list )
        {
            if ( list.IsNull() )
            {
                return 0;
            }
            else if ( !CurState.Index.HasValue )
            {
                return 0;
            }
            int index = CurState.Index.Value;
            decimal num = int.MaxValue;
            foreach ( object[] b in list )
            {
                object i = b[index];
                if ( i == null )
                {
                    continue;
                }
                decimal k = Convert.ToDecimal(i);
                if ( k < num )
                {
                    num = k;
                }
            }
            return num;
        }
        public static decimal Average ( params object[][] list )
        {
            if ( list.IsNull() )
            {
                return 0;
            }
            else if ( !CurState.Index.HasValue )
            {
                return 0;
            }
            decimal num = Sum(list);
            return Math.Round(num / list.Length, 2);
        }
        /// <summary>
        /// 计算同行相加值
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static decimal Sum ( params object[][] list )
        {
            if ( list.IsNull() )
            {
                return 0;
            }
            else if ( !CurState.Index.HasValue )
            {
                return 0;
            }
            int index = CurState.Index.Value;
            decimal num = decimal.Zero;
            foreach ( object[] i in list )
            {
                object t = i[index];
                if ( t == null )
                {
                    continue;
                }
                num += Convert.ToDecimal(t);
            }
            return num;
        }
        /// <summary>
        /// 取乘积
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static decimal Multiply ( params object[][] list )
        {
            if ( list.IsNull() )
            {
                return 0;
            }
            else if ( !CurState.Index.HasValue )
            {
                return 0;
            }
            int index = CurState.Index.Value;
            decimal num = 1;
            foreach ( object[] i in list )
            {
                object t = i[index];
                if ( t == null )
                {
                    continue;
                }
                num *= Convert.ToDecimal(t);
            }
            return num;
        }
        public static object GetValue ( object[] array, int? index )
        {
            if ( array.IsNull() )
            {
                return null;
            }
            else if ( index.HasValue && array.Length > index )
            {
                return array[index.Value];
            }
            else if ( CurState.Index.HasValue )
            {
                return array[CurState.Index.Value];
            }
            return null;
        }
        public static int RowIndex ()
        {
            if ( CurState.Index.HasValue )
            {
                return CurState.Index.Value;
            }
            return -1;
        }
    }
}
